package com.sxhuayuan.parking.aspects;

import javax.annotation.Resource;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.sxhuayuan.parking.compenent.RabbitMQConfig;
import com.sxhuayuan.parking.entity.ParkingIn;
import com.sxhuayuan.parking.entity.ParkingLot;
import com.sxhuayuan.parking.queue.QueueService;
import com.sxhuayuan.parking.queue.msg.ParkingInQueueMsg;
import com.sxhuayuan.parking.queue.msg.QueueMsg;
import com.sxhuayuan.parking.service.MemberService;
import com.sxhuayuan.parking.service.ParkingInService;
import com.sxhuayuan.parking.service.ParkingLotService;
import com.sxhuayuan.parking.service.PartnerpayService;
import com.sxhuayuan.parking.service.PluginService;
import com.sxhuayuan.parking.utils.DateUtils;

/**
 * 停车出入场切面
 */
@Component("parkingInOutAspect")
@Aspect
public class ParkingInOutAspect {

	Logger log = LoggerFactory.getLogger(getClass());

	@Resource(name = "partnerpayServiceImpl")
	PartnerpayService partnerpayService;
	@Resource(name = "parkingInServiceImpl")
	ParkingInService inService;
	@Resource(name = "parkingLotServiceImpl")
	ParkingLotService lotService;
	@Resource(name = "memberServiceImpl")
	MemberService memberService;
	@Resource(name = "pluginServiceImpl")
	PluginService pluginService;
	@Resource(name = "rabbitMsgQueue")
	QueueService rabbitMsgQueue;

	@Pointcut(value = "execution(* com.sxhuayuan.parking.service.ParkingInService.enter(..))")
	public void enterCutPoint() {
	}

	@Pointcut(value = "execution(* com.sxhuayuan.parking.service.ParkingInOutService.out(..))")
	public void outCutPoint() {
	}

	@AfterReturning(value = "enterCutPoint()", returning = "in")
	public void afterEnter(JoinPoint jp, ParkingIn in) {
		ParkingLot lot = lotService.findOne(in.getParkingLotId());
		if (lot.getIsPartnerpayEnable()) {
			log.debug("通知api入场: {}", in.getCarPlate());
			QueueMsg msg = new QueueMsg();
			msg.setRetryPlan(ParkingInQueueMsg.RETRY_PLAN);
			msg.setRetryTimes(0);
			msg.setDelayTime(System.currentTimeMillis());
			msg.setTopic(RabbitMQConfig.QUEUE_PARK_IN);
			msg.setId(in.getUniqueId());
			ParkingInQueueMsg body = new ParkingInQueueMsg();
			body.setCarPlate(in.getCarPlate());
			body.setInTime(DateUtils.format(in.getInTime(), DateUtils.YYYYMMDDHHMMSS));
			body.setFreeMinutes(in.getFreeMinutes());
			body.setParkingLotSn(lot.getSn());
			msg.setBody(JSON.toJSONString(body));
			rabbitMsgQueue.push(msg);
		}
	}

	@AfterReturning(value = "outCutPoint()", returning = "result")
	public void afterOut(JoinPoint jp, ParkingIn result) {

	}
}
